from tkinter import *
from tkinter.messagebox import *


class Claculator_circul(Tk):
    def __init__(self):
        super().__init__()
        self.geometry("300x150+800+400")
        self.title("计算圆的面积和周长")
        self.resizable(0, 0)

        self.pi = 3.1415926

        self.setup_UI()

    def setup_UI(self):
        self.label_01 = Label(self, text="输入圆的半径")
        self.label_01.place(x=10, y=10)

        self.Var_num = Variable()
        self.entry_01 = Entry(self, textvariable=self.Var_num, width=15)
        self.entry_01.place(x=120, y=10)

        self.button_01 = Button(self, text="计算", command=self.check)
        self.button_01.place(x=240, y=10)

        self.label_02 = Label(self, text="圆的面积")
        self.label_02.place(x=10, y=50)

        self.Var_num_a = Variable()
        self.entry_02 = Entry(self,
                              textvariable=self.Var_num_a,
                              state=DISABLED,
                              width=15)
        self.entry_02.place(x=120, y=50)

        self.label_03 = Label(self, text="圆的周长")
        self.label_03.place(x=10, y=90)

        self.Var_num_p = Variable()
        self.entry_03 = Entry(self,
                              textvariable=self.Var_num_p,
                              state=DISABLED,
                              width=15)
        self.entry_03.place(x=120, y=90)

    def show(self):
        self.mainloop()

    def check(self):
        self.num = self.Var_num.get()
        try:
            if self.num == "":
                showinfo("系统消息", "半径不能为空")
            elif type(float(self.num)) is float and float(self.num) > 0:
                self.radius = float(self.num)
                self.area(self.radius)
                self.perimeter(self.radius)
            else:
                showinfo("系统消息", "输入正确的半径，大于0")
        except:
            showinfo("系统消息", "输入正确的半径，大于0")

    def area(self, radius):
        self.radius = radius
        result_a = self.pi * (self.radius**2)
        self.Var_num_a.set("%.2f"%result_a)

    def perimeter(self, radius):
        self.radius = radius
        result_p = 2 * self.pi * self.radius
        self.Var_num_p.set("%.2f"%result_p)

    def error_input(self):
        showinfo("系统消息", "输入正确的半径，大于0")


if __name__ == "__main__":
    this_gui = Claculator_circul()
    this_gui.show()